<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设置名称" prop="settingName">
        <el-input
          v-model="queryParams.settingName"
          placeholder="请输入设置名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纸张尺寸" prop="paperSize">
        <el-select
          v-model="queryParams.paperSize"
          placeholder="请选择纸张尺寸"
          clearable
          style="width: 120px"
        >
          <el-option label="A3" value="A3"></el-option>
          <el-option label="A4" value="A4"></el-option>
          <el-option label="A5" value="A5"></el-option>
          <el-option label="B4" value="B4"></el-option>
          <el-option label="B5" value="B5"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="纸张方向" prop="paperOrientation">
        <el-select
          v-model="queryParams.paperOrientation"
          placeholder="请选择纸张方向"
          clearable
          style="width: 120px"
        >
          <el-option label="纵向" value="portrait"></el-option>
          <el-option label="横向" value="landscape"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="左边距(mm)" prop="marginLeft">
        <el-input
          v-model="queryParams.marginLeft"
          placeholder="请输入左边距(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上边距(mm)" prop="marginTop">
        <el-input
          v-model="queryParams.marginTop"
          placeholder="请输入上边距(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字体名称" prop="fontName">
        <el-input
          v-model="queryParams.fontName"
          placeholder="请输入字体名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字体大小(pt)" prop="fontSize">
        <el-input
          v-model="queryParams.fontSize"
          placeholder="请输入字体大小(pt)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题字体大小(pt)" prop="titleFontSize">
        <el-input
          v-model="queryParams.titleFontSize"
          placeholder="请输入标题字体大小(pt)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="LOGO图片路径" prop="logoPath">
        <el-input
          v-model="queryParams.logoPath"
          placeholder="请输入LOGO图片路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否默认" prop="isDefault">
        <el-select
          v-model="queryParams.isDefault"
          placeholder="请选择是否默认"
          clearable
          style="width: 120px"
        >
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          style="width: 120px"
        >
          <el-option label="正常" value="1"></el-option>
          <el-option label="停用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['nongjt:setting:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nongjt:setting:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nongjt:setting:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['nongjt:setting:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="settingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="设置名称" align="center" prop="settingName" />
      <el-table-column label="纸张尺寸" align="center" prop="paperSize" />
      <el-table-column label="纸张方向" align="center" prop="paperOrientation" />
      <el-table-column label="字体大小(pt)" align="center" prop="fontSize" />
      <el-table-column label="是否默认" align="center" prop="isDefault">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isDefault === 1" type="success">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['nongjt:setting:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['nongjt:setting:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合格证打印设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设置名称" prop="settingName">
              <el-input v-model="form.settingName" placeholder="请输入设置名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模板类型" prop="templateType">
              <el-input v-model="form.templateType" placeholder="请输入模板类型" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="纸张尺寸" prop="paperSize">
              <el-select v-model="form.paperSize" placeholder="请选择纸张尺寸">
                <el-option label="A3" value="A3"></el-option>
                <el-option label="A4" value="A4"></el-option>
                <el-option label="A5" value="A5"></el-option>
                <el-option label="B4" value="B4"></el-option>
                <el-option label="B5" value="B5"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纸张方向" prop="paperOrientation">
              <el-select v-model="form.paperOrientation" placeholder="请选择纸张方向">
                <el-option label="纵向" value="portrait"></el-option>
                <el-option label="横向" value="landscape"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="左边距(mm)" prop="marginLeft">
              <el-input-number v-model="form.marginLeft" :min="0" :max="100" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上边距(mm)" prop="marginTop">
              <el-input-number v-model="form.marginTop" :min="0" :max="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="字体名称" prop="fontName">
              <el-input v-model="form.fontName" placeholder="请输入字体名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字体大小(pt)" prop="fontSize">
              <el-input-number v-model="form.fontSize" :min="1" :max="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题字体大小(pt)" prop="titleFontSize">
              <el-input-number v-model="form.titleFontSize" :min="1" :max="100" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否默认" prop="isDefault">
              <el-radio-group v-model="form.isDefault">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="LOGO图片路径" prop="logoPath">
              <el-input v-model="form.logoPath" placeholder="请输入LOGO图片路径" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio :label="1">正常</el-radio>
                <el-radio :label="0">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSetting, getSetting, delSetting, addSetting, updateSetting } from "@/api/nongjt/setting"

export default {
  name: "Setting",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合格证打印设置表格数据
      settingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        settingName: null,
        templateType: null,
        paperSize: null,
        paperOrientation: null,
        marginLeft: null,
        marginTop: null,
        fontName: null,
        fontSize: null,
        titleFontSize: null,
        logoPath: null,
        isDefault: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        settingName: [
          { required: true, message: "设置名称不能为空", trigger: "blur" }
        ],
        templateType: [
          { required: true, message: "模板类型不能为空", trigger: "change" }
        ],
        paperSize: [
          { required: true, message: "纸张尺寸不能为空", trigger: "blur" }
        ],
        paperOrientation: [
          { required: true, message: "纸张方向不能为空", trigger: "blur" }
        ],
        isDefault: [
          { required: true, message: "是否默认不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询合格证打印设置列表 */
    getList() {
      this.loading = true
      listSetting(this.queryParams).then(response => {
        this.settingList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        settingName: null,
        templateType: null,
        paperSize: null,
        paperOrientation: null,
        marginLeft: null,
        marginTop: null,
        fontName: null,
        fontSize: null,
        titleFontSize: null,
        logoPath: null,
        isDefault: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加合格证打印设置"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSetting(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改合格证打印设置"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSetting(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addSetting(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除合格证打印设置编号为"' + ids + '"的数据项？').then(function() {
        return delSetting(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('nongjt/setting/export', {
        ...this.queryParams
      }, `setting_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
